package com.hxd.otc.module.my.fragment;

import android.os.Bundle;

import com.hxd.otc.base.BaseRecyclerFragment;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.NoticeType;
import com.hxd.otc.module.my.activity.AfficheDetailActivity;
import com.hxd.otc.module.my.adapter.AfficheAdapter;
import com.hxd.otc.module.my.bean.NoticeBean;
import com.hxd.otc.module.my.contract.AfficheContract;
import com.hxd.otc.module.my.presenter.AffichePresenter;

import java.util.List;

/**
 * Created by lzy on 2018/6/15 0015.
 * description ${公告列表}
 */
public class AfficheFragment extends BaseRecyclerFragment<AffichePresenter, AfficheAdapter, NoticeBean> implements AfficheContract.View {
    public static final String EXTRA_NOTIFY_TYPE = "notice_type";

    @Override
    protected AfficheAdapter createAdapter() {
        return new AfficheAdapter(getContext());
    }

    @Override
    protected AffichePresenter createPresenter() {
        Bundle arguments = getArguments();
        NoticeType noticeType = null;
        if (arguments != null) {
            noticeType = (NoticeType) arguments.getSerializable(EXTRA_NOTIFY_TYPE);
        }
        if (noticeType == null) noticeType = NoticeType.MESSAGE;
        AffichePresenter affichePresenter = new AffichePresenter(this);
        affichePresenter.setNotifyType(noticeType);
        return affichePresenter;
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
        Bundle bundle = new Bundle();
        NoticeBean item = mAdapter.getItem(position);
        bundle.putSerializable(AfficheDetailActivity.EXTRA_MESSAGE_DETAIL, item);
        bundle.putInt(AfficheDetailActivity.EXTRA_POSITION, position);
        $startActivity(AfficheDetailActivity.class, bundle);
        if (UserHelper.isLogin() && !item.isLook()) {
            mPresenter.lookNotice(item);
        }
    }

    @Override
    public void lookNoticeSuccess(NoticeBean noticeBean) {
        List<NoticeBean> items = mAdapter.getItems();
        for (int i = 0; i < items.size(); i++) {
            NoticeBean item = items.get(i);
            if (noticeBean.equals(item)) {
                item.setLook(true);
                mAdapter.notifyItemChanged(i);
                break;
            }
        }
    }

    @Override
    public void lookNoticeFail(String errMessage) {
    }
}
